﻿using System;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using Relocation.Com;
using Relocation.Base;

namespace Relocation
{
	static class Program
	{
		public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main()
		{
			Log.Info("Relocation Client Start.");

			try
			{
				if (IsRun())
				{
					MyMessagebox.Show("程序已经运行！");
					return;
				}

				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);

				Session session = new Session();
				session.DataModel = new Relocation.Data.DataModel();

				LoginWindow loginWin = new LoginWindow(session);
				if (loginWin.ShowDialog().IsOK() && loginWin.User != null)
				{
					session.User = loginWin.User;
					MainWindow mainWin = new MainWindow(session);
					session.Map.Add(Session.KEY_MAIN_WINDOW, mainWin);
					Application.Run(mainWin);
				}
			} catch (Exception ex)
			{	
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				MyMessagebox.Show("系统错误！");
			}

			Log.Info("Relocation Client End.");
		}

		static bool IsRun()
		{
			string proc = Process.GetCurrentProcess().ProcessName;
			Process[] processes = Process.GetProcessesByName(proc);
			return processes.Length > 1;
		}

	}
}
